package com.lbg.drug.controller;


import cn.hutool.core.util.ObjectUtil;
import com.lbg.drug.commont.untils.R;
import com.lbg.drug.dto.Admindto;
import com.lbg.drug.dto.Adminupdate;
import com.lbg.drug.entity.Admin;
import com.lbg.drug.service.AdminService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author1 lbg
 * @since 2022-04-26
 */

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;


    //    @PostMapping(value = "/login")
//    public R login(String username, String password, String captcha, HttpServletRequest request) {
//        UserEntity user = userService.selectOne(new EntityWrapper<UserEntity>().eq("username", username));
//        if(user==null || !user.getPassword().equals(password)) {
//            return R.error("账号或密码不正确");
//        }
//        String token = tokenService.generateToken(user.getId(),username, "users", user.getRole());
//        return R.ok().put("token", token);
//    }
    @PostMapping("/getUser")
    public R getUser(HttpSession session, @RequestBody Admindto admindto) {



        if (StringUtils.isEmpty(admindto.getUserId()) || StringUtils.isEmpty(admindto.getPassword())) {

            return R.error("密码或账户有误！！");
        }else if (adminService.getLogin(admindto) == null){
            return R.error("密码或账户有误！！");
        }
        else {
            session.setAttribute("Admin",admindto);
            return R.ok("登录成功！");
        }
    }


    @PostMapping("/UpdateUser")
    public R UpdateUser(@RequestBody Adminupdate adminupdate) {
        adminService.update(adminupdate);
        return R.ok("修改成功！");

    }

    @PostMapping("/adduser")
    public R adduser(@RequestBody Adminupdate admin) {
        adminService.add(admin);
        return R.ok("添加成功！");

    }


}

